package com.playball.lottery;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.playball.lottery.R;
import com.playball.lottery.adapter.ViewPagerAdapter;
import com.playball.lottery.base.BaseActivity;

/**
 * @author tanglizhong
 * @version 2014-5-6 下午4:56:56
 */
public class GuideActivity extends BaseActivity implements OnPageChangeListener {
	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;

	// 底部小点图片
	private ImageView[] dots;

	// 记录当前选中位置
	private int currentIndex;

	private LinearLayout ll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		// 初始化页面
		initViews();

		// 初始化底部小点
		initDots();
	}

	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList<View>();
		// 初始化引导图片列表
		
		for(int i=1; i<=3; i++) {
			View view = inflater.inflate(R.layout.activity_guide_02, null);
			switch(i) {
			case 1:
				view.findViewById(R.id.iv_guide).setBackgroundResource(R.drawable.guide01);
				break;
			case 2:
				view.findViewById(R.id.iv_guide).setBackgroundResource(R.drawable.guide02);
				break;
			case 3:
				view.findViewById(R.id.iv_guide).setBackgroundResource(R.drawable.guide03);
//				view.findViewById(R.id.btn_start_main).setVisibility(View.VISIBLE);
				break;
			}
			views.add(view);
		}

		// 初始化Adapter
		vpAdapter = new ViewPagerAdapter(views, this);

		vp = (ViewPager) findViewById(R.id.viewpager);
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
	}

	private void initDots() {
		LayoutInflater inflater = LayoutInflater.from(this);
		
		ll = (LinearLayout) findViewById(R.id.ll);

		dots = new ImageView[views.size()];

		// 循环取得小点图片
		for (int i = 0; i < views.size(); i++) {
//			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i] = (ImageView) inflater.inflate(R.layout.image_guide, null);
			dots[i].setEnabled(true);// 都设为灰色
			ll.addView(dots[i]);
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}

	private void setCurrentDot(int position) {
		if (position < 0 || position > views.size() - 1 || currentIndex == position) {
			return;
		}

		if (position == views.size() - 1) {
			ll.setVisibility(View.GONE);
		} else {
			ll.setVisibility(View.VISIBLE);
		}

		dots[position].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = position;
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
	}

	public void onResume() {
		super.onResume();
	}

	public void onPause() {
		super.onPause();
	}
}
